<?php
	include_once("CONN.php");
	
	session_start();
	$pic = "";
	if(isset($_POST["issue"]) && $_POST["issue"] == "发布"){
		
		if($_POST["sub"] == "失物招领"){
			$area = "lost";
		}
		if($_POST["sub"] == "闲置转让"){
			$area = "transfer";
		}
		if($_POST["sub"] == "提问"){
			$area = "problem";
		}
		if($_POST["sub"] == "表白"){
			$area = "love";
		}
		if($_POST["sub"] == "组队"){
			$area = "team";
		}
		if($_POST["sub"] == "吐槽"){
			$area = "debate";
		}
		if($_FILES['upPic']['name']){
			if($_FILES['upPic']['type'] == "image/jpg" || $_FILES['upPic']['type'] == "image/png" || $_FILES['upPic']['type'] == "image/gif" || $_FILES['upPic']['type'] == "image/jpeg" || $_FILES['upPic']['type'] == "image/pjpeg" || $_FILES['upPic']['type'] == "image/x-png"){
				if($_FILES['upPic']['size'] < 204800 && $_FILES['upPic']['size'] > 0){
					$pic = "../".$_FILES['upPic']['name'];
					move_uploaded_file($_FILES['upPic']['tmp_name'], $pic);
					$matter = $_POST["content"];
					$time = date('Y-m-d H:i:s',time());
					$author = $_SESSION["userName"];
					$likes = 0;
					$sqlstr = 
					"insert message set area='".$area."',matter='".$matter."',uptime='".$time."',author='".$author."',pic='".$pic."',likes='".$likes."'";
					$result = mysqli_query($connID,$sqlstr);
					if($result){
						echo"<script>alert ('发布成功！')</script>";
						echo "<script> window.location.href='../HTML/UpMassage.html' </script>";
					}else{
						echo"<script>alert ('发布失败！请检查网络')</script>";
						echo "<script> window.location.href='../HTML/UpMassage.html' </script>";
					}
				}else{
					echo"<script>alert ('请上传图片！小于200kb!')</script>";
				echo "<script> window.location.href='../HTML/UpMassage.html' </script>"; 
				}	
			}else{
				echo"<script>alert ('请上传文件为图片！')</script>";
				echo "<script> window.location.href='../HTML/UpMassage.html' </script>"; 
			}
		}
		
		
	}
	if(isset($_POST["issue0"]) && $_POST["issue0"] == "匿名发布"){
		if($_POST["sub"] == "失物招领"){
			$area = "lost";
		}
		if($_POST["sub"] == "闲置转让"){
			$area = "transfer";
		}
		if($_POST["sub"] == "提问"){
			$area = "problem";
		}
		if($_POST["sub"] == "表白"){
			$area = "love";
		}
		if($_POST["sub"] == "组队"){
			$area = "team";
		}
		if($_POST["sub"] == "吐槽"){
			$area = "debate";
		}
		if($_FILES['upPic']['name']){
			if($_FILES['upPic']['type'] == "image/jpg" || $_FILES['upPic']['type'] == "image/png" || $_FILES['upPic']['type'] == "image/gif" || $_FILES['upPic']['type'] == "image/jpeg" || $_FILES['upPic']['type'] == "image/pjpeg" || $_FILES['upPic']['type'] == "image/x-png"){
				if($_FILES['upPic']['size'] < 204800 && $_FILES['upPic']['size'] > 0){
					$pic = "../".$_FILES['upPic']['name'];
					move_uploaded_file($_FILES['upPic']['tmp_name'], $pic);
					$author = "匿名用户";
					$matter = $_POST["content"];
					$time = date('Y-m-d H:i:s',time());
					$sqlstr = 
					"insert message set area='".$area."',matter='".$matter."',uptime='".$time."',author='".$author."',pic='".$pic."'";
					$result = mysqli_query($connID,$sqlstr);
					if($result){
						echo"<script>alert ('发布成功！')</script>";
						echo "<script> window.location.href='../HTML/UpMassage.html' </script>";
					}else{
						echo"<script>alert ('发布失败！请检查网络')</script>";
						echo "<script> window.location.href='../HTML/UpMassage.html' </script>";
					}
				}else{
					echo"<script>alert ('请上传图片！小于200kb!')</script>";
				echo "<script> window.location.href='../HTML/UpMassage.html' </script>"; 
				}	
			}else{
				echo"<script>alert ('请上传文件为图片！')</script>";
				echo "<script> window.location.href='../HTML/UpMassage.html' </script>"; 
			}
		}
		

	}
	if(isset($_POST["save"]) && $_POST["save"] == "保存"){

		if($_POST["sub"] == "失物招领"){
			$area = "lost";
		}
		if($_POST["sub"] == "闲置转让"){
			$area = "transfer";
		}
		if($_POST["sub"] == "提问"){
			$area = "problem";
		}
		if($_POST["sub"] == "表白"){
			$area = "love";
		}
		if($_POST["sub"] == "组队"){
			$area = "team";
		}
		if($_POST["sub"] == "吐槽"){
			$area = "debate";
		}
		$matter = $_POST["content"];
		$time = date('Y-m-d H:i:s',time());
		$author = $_SESSION["userName"];
		$_SESSION["sub"] = $area;
		$_SESSION["content"] = $matter;
		$sqlstr = 
		"insert save set area='".$area."',matter='".$matter."',uptime='".$time."',author='".$author."'";
		$result = mysqli_query($connID,$sqlstr);
		//echo "ssssssssssss";
		if($result){
			echo"<script>alert ('保存成功！')</script>";
			$_POST["content"] = $_SESSION["content"];
			$_POST["sub"] = $_SESSION["sub"];
			echo "<script> window.location.href='../HTML/UpMassage.html' </script>";
			

		}else{
			echo"<script>alert ('保存失败！请检查网络')</script>";
			echo "<script> window.location.href='../HTML/UpMassage.html' </script>";
		}
	}
?>